package cn.tedu.zx.order.adapter.consumer;


import cn.tedu.zx.order.service.BookingService;
import cn.tedu.zx.pojo.order.dto.BookingAddNewDto;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;


@Component
@RocketMQMessageListener(
        topic = "booking-topic",
        consumerGroup = "order-consumer",
        selectorExpression = "*")
@Slf4j
public class OrderConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    BookingService bookingService;

    public OrderConsumer() {
        log.debug("启动消费者");
    }

    @Override
    public void onMessage(MessageExt messageExt) {

        String msgId = messageExt.getMsgId();
        byte[] body = messageExt.getBody();
        String json = new String(body, StandardCharsets.UTF_8);
        log.debug("消费消息{}",json);
        BookingAddNewDto bookingAddNewDto = JSON.parseObject(json, BookingAddNewDto.class);
        bookingService.addNew(bookingAddNewDto);
        log.debug("完成添加{}",bookingAddNewDto);
    }
}
